#coding=utf-8
'''
Created on 2015年3月19日

@author: 403720
'''
import urllib
import httplib
import json
import os
import os.path
import urlparse
import time
import random
#导入logging保存和输出文件模板 import logging_save
import logging_save
import re
import serial
import logging
'''
    value:
1    short        单击
2    double       双击
3    long         长按
4    longlong    超长按
'''
class presskey(object):
    #构造函数定义，确定连接基本信息
    def __init__(self,conn=httplib.HTTPConnection('192.168.1.101'),headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"}):
        self.Connection=conn  
        self.Headers=headers
    
    def signal_short(self):
        logging_save.logging.info("现在开始单按short press键测试")
        logging_save.logging.info("将会测试十次按键")
        #需要先确保播放处于播放状态，呵呵，断按1的话判断是处于暂停，再短按1是处于播放。 先判断下播放状态
        if get_status(self)==1:
            logging_save.logging.info('当前状态处于播放状态')
            #设置循环播放，假设已经处于播放状态
            for val in range(10):
                time.sleep(5)
                set_level(presskey_test,1)
                
                if get_status(self)==2:
                    logging_save.logging.info('已经处于暂停状态')
                    time.sleep(5)
                else:
                    logging_save.logging.info("FAIL,FAIL,FAIL,应该要求处于暂停状态")    
                    
                set_level(presskey_test,1)
                
                if get_status(self)==1:
                    logging_save.logging.info('已经处于播放状态')
                    time.sleep(5)
                else:
                    logging_save.logging.warning("FAIL,FAIL,FAIL,未进入播放状态")
                
        else:
            logging_save.logging.info('当前状态处于暂停或者非播放状态')
            #设置循环播放，假设已经处于播放状态
            for val in range(5):
                
                set_level(presskey_test,1)
                
            
                if get_status(self)==1:
                    logging_save.logging.info('已经处于播放状态')
                    time.sleep(5)
                else:
                    logging_save.logging.info("FAIL,FAIL,FAIL,应该要求处于播放状态")    
                    
                set_level(presskey_test,1)
                
                if get_status(self)==2:
                    logging_save.logging.info('已经处于暂停状态')
                    time.sleep(3)
                else:
                    logging_save.logging.warning("FAIL,FAIL,FAIL,未进入暂停状态")
                    
                    
                    
    def double_short(self):
        logging_save.logging.info("现在开始double press键测试")
        if get_status(self)==1:
            logging_save.logging.info('当前状态处于播放状态')
            #设置循环播放，假设已经处于播放状态
            for val in range(10):
                time.sleep(5)
                #设置double press按键
                set_level(presskey_test,2)
                
                time.sleep(3)
                if get_status(self)==1:
                    logging_save.logging.info('已经处于播放状态')
                    time.sleep(5)
                else:
                    logging_save.logging.info("FAIL,FAIL,FAIL,应该要求处于播放状态")    
                    
                
        else:
            logging_save.logging.info('当前状态处于暂停或者非播放状态')
            #设置循环播放，假设已经处于播放状态
            for val in range(10):
                time.sleep(5)
                #double press
                set_level(presskey_test,2)
                time.sleep(3)
                if get_status(self)==2:
                    logging_save.logging.info('已经处于暂停状态')
                    time.sleep(5)
                else:
                    logging_save.logging.info("FAIL,FAIL,FAIL,应该要求处于暂停状态")    
                             
                   
              
def set_level(self,presslevel):
    postbody_level={"command":"phy-key","value":presslevel}
    postjson=json.dumps(postbody_level)
    self.Connection.request('POST', '/api/phy-key',postjson,self.Headers)
    logging_save.logging.info("物理按键将要按键为："+str(presslevel))
    self.Connection.close()  
    

def get_status(self):
    self.Connection.request('GET', '/api/request-pb')
    getinformation=self.Connection.getresponse()
    readinformation=getinformation.read()
    #print readinformation
    getdeviceinfo=eval(readinformation)
    current_playstatus=getdeviceinfo['p_status']
    #p_status为1：播放，p_status为2，暂停， 3，停止，4，播放结束（只适用于网络播放）
    return current_playstatus
    self.Connection.close()

presskey_test=presskey()
presskey_test.signal_short()
presskey_test.double_short()


